Skip to content

Sequence equality #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 71 commits into from
Feb 24, 2025
Merged

Sequence equality #249

merged 71 commits into from
Feb 24, 2025

Conversation

frenchy64
Copy link
Contributor

@frenchy64 frenchy64 commented Feb 7, 2025

Closes #243
Closes #250
Closes #251
Closes #274
Closes #283

It would be nice to replace nullptr returns with nil in sequenceable but it enables important optimizations for calls to sequenceable member functions. I did not investigate returning option types.

@frenchy64 frenchy64 changed the title [wip] range equality [wip] sequence equality Feb 7, 2025
Copy link
Member

@jeaye jeaye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of good fixes in here. I think there are some introduced bugs though. I tried to pick them out.

@frenchy64
Copy link
Contributor Author

frenchy64 commented Feb 23, 2025

Found and fixed a couple more issues while I scanned everything. Lmk if I should pull them out.

  • apply_to isn't compatible with seqs that return fresh seqs from next_in_place. Not sure if they exist, but persistent_list is implemented that way.
  • removed transient cases to resolve Transients mutate in-place on persistent ops #274
    • extra: conj case for seqables didn't seem right to me compared to Clojure, so I removed it. For one, it changes the type of the seqable to a seq.
  • added paranoid assertions for nil while the function/template ambiguity exists for runtime::{next,seq,next_in_place,fresh_seq}.

@frenchy64 frenchy64 requested a review from jeaye February 23, 2025 07:54
Copy link
Member

@jeaye jeaye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly a rehash of our slack discussion, but also called out some other concerns.

@@ -878,88 +878,88 @@ namespace jank::runtime
case 1:
return dynamic_call(source, s->first());
case 2:
return dynamic_call(source, s->first(), s->next_in_place()->first());
return dynamic_call(source, s->first(), (s = s->next_in_place())->first());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a local TODO that notes that persistent_list_sequence shouldn't exist, since persistent_list itself is a sequence.

This change is a big red flag, since this is an extremely hot code path. I don't want to impact its perf for this one case which needs to change anyway.

If you want to add a comment here noting that it doesn't work for persistent_list, that's ok. If you want to remove persistent_list_sequence, that's also ok, but let's do it in a separate PR.

Either way, let's please undo this file's changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, let's please undo this file's changes.

Sure.

I don't want to impact its perf for this one case which needs to change anyway.

I don't think the bug is reachable right now.

persistent_list::next_in_place is the potential problem, but it's currently unreachable to apply_to since a fresh seq is taken and then persistent_list_sequence::next_in_place is called instead.

I'm not sure what would happen if persistent_list_sequence were removed and (presumably) replaced with persistent_list itself. I think the apply_to bug would be introduced if persistent_list::next_in_place continued to allocate a new list, as is implied in this comment.

  persistent_list_sequence_ptr persistent_list::next_in_place() const
  {
    /* In-place updates don't make sense for lists, since any call to fresh_seq would return
     * a list sequence. So we know, principally, that a list itself cannot be considered fresh. */
    return next();
  }

Copy link
Member

@jeaye jeaye Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment would no longer apply, if we removed persistent_list_sequence. This would be updated to actually mutate the list.

else if(!rhs)
if(!rhs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the else? I find it helps with the logical flow. Also, by having the else if, your assertion is proven by the compiler to be redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added back the else. Since the compiler pointed out the assertion was redundant, does that mean returning false is equivalent to returning !lhs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, equivalent. Was just like that for symmetry.

Comment on lines 165 to 166
assert(it != nil::nil_const());
assert(seq != nil::nil_const());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's impossible for these assertions to be false, proven by the type system. I think you're concerned about the next_in_place(it) call somehow returning an object_ptr. But it could never do that, since it would result in a compiler error.

Now, the history is that next_in_place has been in flux for a year or so and I was making some changes which would allow for it to be optional. That never happened, but it's why we're not using it->next_in_place() here. Ultimately, it's doing the same thing and all of these can be changed back. I just haven't done that yet, since it's a perf change and I haven't been focusing on perf.

Either way, your concerns about this being nil should not be keeping you up. auto it(fresh_seq()); means that we'll have a fully typed it for the currently visited object. We cannot assign an object_ptr to it. C++ won't allow it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. What about something like this loop? Can this be nil?

fn->arity_3 = [](object * const l, object * const r, object * const rest) -> object * {
if(!is_equiv(l, r))
{
return obj::boolean::false_const();
}
for(auto it(fresh_seq(rest)); it != nullptr; it = next_in_place(it))

Copy link
Contributor Author

@frenchy64 frenchy64 Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eep.

clojure.core=> (== 1 1 1 1 1)
Exception: not a number: nil

Opened #283

Included fix here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That one can be nil, yes, since it's using the object_ptr overloads. However, it cannot be nullptr, since those functions never return nullptr.

@@ -16,6 +16,7 @@ namespace jank::runtime::obj
: value{ value }
, count{ count }
{
assert(0 < to_int(count));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like this could be an exception, rather than assertion, since someone embedding jank may create it a repeat with 0 and we don't need to kill the whole program.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

It doesn't necessarily need to be an error either. We can made these constructors robust by adding checks in its members.

@frenchy64 frenchy64 requested a review from jeaye February 24, 2025 00:39
Copy link
Member

@jeaye jeaye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good with your previous changes. The new bug fix has a comment, though. I'll fix it locally.

Thanks for the iteration, Ambrose!

@@ -578,7 +578,8 @@ jank_object_ptr jank_load_clojure_core_native()
return obj::boolean::false_const();
}

for(auto it(fresh_seq(rest)); it != nullptr; it = next_in_place(it))
for(auto it(fresh_seq(rest)); it != nullptr && it != obj::nil::nil_const();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, here, it cannot be nullptr. That's not part of the contract. Everything returned from runtime:: fns should be non-null.

The one exception is the next_in_place and fresh_seq templates. Ultimately, we can remove those (not in this PR); they're only there for a historical partial refactor which never panned out.

There should never be a case, right now, where we need to check for nullptr and nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should never be a case, right now, where we need to check for nullptr and nil.

There's a few more in multi_function.cpp.

@jeaye jeaye merged commit 6371c32 into jank-lang:main Feb 24, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants